//
//  FifthViewFooterView.m
//  A段项目
//
//  Created by dllo on 16/1/28.
//  Copyright © 2016年 Scott. All rights reserved.
//

#import "FifthViewFooterView.h"
#import "Fifth.h"
#import "ChooseLocalPhotoLibView.h"
#import "EditInfoController.h"
#import "CollectViewController.h"
#import "FMDBDataHandler.h"





@interface FifthViewFooterView ()

@property (nonatomic, retain) UIButton *button1;

@property (nonatomic, retain) UIButton *button3;
@property (nonatomic, retain) UIButton *button4;
@property (nonatomic, retain) NSMutableArray *arrImage;
@property (nonatomic, retain) NSMutableArray *arrayForSmall;




@end

@implementation FifthViewFooterView

- (void)dealloc {
    
     [[NSNotificationCenter defaultCenter] removeObserver:self];
    
    [_imageOfHeader release];
     [_button1 release];
     [_button2 release];
     [_button3 release];
     [_button4 release];
    [_arrImage release];
    
    [super dealloc];
   
    
}

#pragma mark -- 添加控件.
- (instancetype)initWithReuseIdentifier:(NSString *)reuseIdentifier {
    
    self = [super initWithReuseIdentifier:reuseIdentifier];
    
    if (self) {
        
            self.button1 = [UIButton buttonWithType:UIButtonTypeSystem];
            
            [self.contentView addSubview:self.button1];
        
        self.imageOfHeader = [[UIImageView alloc] init];
        
        [self.contentView addSubview:self.imageOfHeader];
        
            self.button2 = [UIButton buttonWithType:UIButtonTypeSystem];
        
            [self.contentView addSubview:_button2];
        
            self.button3 = [UIButton buttonWithType:UIButtonTypeSystem];
        
            [self.contentView addSubview:self.button3];
        
            self.button4 = [UIButton buttonWithType:UIButtonTypeSystem];
        
            [self.contentView addSubview:self.button4];
        
    }
    
    return self;
    
}

#pragma mark -- 布局子视图.
- (void)layoutSubviews {
    
    [super layoutSubviews];
    
    
#pragma mark -- 计算图片的个数.
    PHFetchOptions *fethOptions = [[PHFetchOptions alloc] init];
    
    PHFetchResult *result= [PHAsset fetchAssetsWithMediaType:1 options:fethOptions];
    
    [fethOptions release];
    
    self.contentView.backgroundColor = [UIColor grayColor];
    self.button1.frame = CGRectMake(20, 0, (self.frame.size.width - 100)/4 - 20, self.frame.size.height);
 
    [self.button1 setTitle:[NSString stringWithFormat:@"%ld            本地",result.count] forState:UIControlStateNormal];
    
    [self.button1 addTarget:self action:@selector(button1Action:) forControlEvents:UIControlEventTouchUpInside];  //添加点击事件
    
    self.imageOfHeader.frame = CGRectMake(self.frame.size.width - 98, -42, 84, 84);
    
    self.imageOfHeader.backgroundColor =[ UIColor whiteColor];
    
    self.imageOfHeader.userInteractionEnabled = YES;
    
    self.imageOfHeader.layer.cornerRadius = 42;
    
    self.imageOfHeader.layer.masksToBounds = YES;
    
    NSString *file = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)lastObject]stringByAppendingPathComponent:@"header.jpg"];
    
    UIImage *image = [UIImage imageWithContentsOfFile:file];
    
    if (image == nil) {
        
        self.imageOfHeader.image = [UIImage imageNamed:@"ren"];
        
        
    }else {
        
        self.imageOfHeader.image = image;
        
    }
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)];
    
    [self.imageOfHeader addGestureRecognizer:tap];
    
    [tap release];
    
    
    self.button1.titleLabel.numberOfLines = 2;
    self.button1.titleLabel.textAlignment = 1;
    
   
    self.button1.tintColor = [UIColor whiteColor];
    self.button2.tintColor = [UIColor whiteColor];
    self.button3.tintColor = [UIColor whiteColor];
    self.button4.tintColor = [UIColor whiteColor];
    
    
    [PHPhotoLibrary requestAuthorization:^(PHAuthorizationStatus status) {
        
       [self.button2 setTitle:[NSString stringWithFormat:@"%ld         相册",status] forState: UIControlStateNormal];
        
        self.button2.titleLabel.numberOfLines = 2;
        self.button2.titleLabel.textAlignment = 1;

        
    }];

    self.button2.frame = CGRectMake((self.frame.size.width - 100)/4 + 20, 0, (self.frame.size.width -100)/4 - 20, self.frame.size.height);
    
    [self.button2 addTarget:self action:@selector(button2Action:) forControlEvents:UIControlEventTouchUpInside];
    
    
    NSArray *array = [[FMDBDataHandler shareDataBaseHandler]selectedInfo:200];
    
    
    self.button3.titleLabel.numberOfLines = 2;
    self.button3.titleLabel.textAlignment = 1;

    
    [self.button3 setTitle:[NSString stringWithFormat:@"%ld            收藏", array.count]  forState: UIControlStateNormal];
    
    
    /**删除收藏的照片是刷新数据. */
    [[NSNotificationCenter defaultCenter]addObserverForName:@"refreshCollectCount" object:nil queue:[NSOperationQueue mainQueue] usingBlock:^(NSNotification * _Nonnull note) {
        
        NSArray *array = [[FMDBDataHandler shareDataBaseHandler]selectedInfo:200];

         [self.button3 setTitle:[NSString stringWithFormat:@"%ld            收藏", array.count]  forState: UIControlStateNormal];
        
    }];
    
   
    
    
    self.button3.frame = CGRectMake((2*(self.frame.size.width - 100)/4) + 20, 0, (self.frame.size.width -100)/4 - 20, self.frame.size.height);
    
    [self.button3 addTarget:self action:@selector(button3Action:) forControlEvents:UIControlEventTouchUpInside];
    
    
    self.button3.titleLabel.numberOfLines = 2;
    self.button3.titleLabel.textAlignment = 1;
    
    
    [self.button4 setTitle:@"0           圈子" forState: UIControlStateNormal];
    self.button4.frame = CGRectMake((3*(self.frame.size.width - 100 )/4) + 20, 0, (self.frame.size.width - 100)/4 - 20, self.frame.size.height);
    
    [self.button4 addTarget:self action:@selector(button4Action:) forControlEvents:UIControlEventTouchUpInside];
    
    self.button4.titleLabel.numberOfLines = 2;
    self.button4.titleLabel.textAlignment = 1;
    
    
    
#pragma mark --换头像.
    /**接收头像改变的通知.*/
    [[NSNotificationCenter defaultCenter] addObserverForName:@"headerImageChange" object:nil queue:[NSOperationQueue mainQueue] usingBlock:^(NSNotification * _Nonnull note) {
        
        NSString *file = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)lastObject]stringByAppendingPathComponent:@"header.jpg"];
        
        UIImage *image = [UIImage imageWithContentsOfFile:file];
        
        self.imageOfHeader.image = image;
        
    }];
    

    
    
}

/**推出编辑界面. */
- (void)tapAction:(UITapGestureRecognizer *)tap {
    
    id object = [self nextResponder];
    
    while (![object isKindOfClass:[Fifth class]] &&object != nil) {
        
        object = [object nextResponder];
        
    }
    Fifth *fif = (Fifth *)object;
    
    EditInfoController *edit = [[EditInfoController alloc] init];
    
    [fif.navigationController pushViewController:edit animated:YES];
    
    [edit release];
    

}

#pragma mark -- 本地的点击事件
- (void)button1Action:(UIButton *)button {
    
    self.arrImage = [NSMutableArray array];  // 放获取的照片的数组
    
    PHFetchOptions *fethOptions = [[PHFetchOptions alloc] init];
    
    PHFetchResult *result1= [PHAsset fetchAssetsWithMediaType:1 options:fethOptions];//得到一个照片列表.
    
    NSMutableArray *array = [NSMutableArray array];
 /**枚举列表,并添加到数组里.*/
   [result1 enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
       
       [array addObject:obj];
       
   }];
    
   
    static PHImageRequestOptions *requestOptions;
    
    if (!requestOptions) {
        
        requestOptions = [[PHImageRequestOptions alloc] init];
        
        requestOptions.deliveryMode = 2;
        
        requestOptions.resizeMode =1;
    }
    
   /* 图片缩略图.*/
    
    PHCachingImageManager *cache = [[PHCachingImageManager alloc] init];
    
    /**当数组不为空时开始遍历. */
    for (int i = 0; i < array.count; i++) {
    
        [cache requestImageForAsset:array[i] targetSize:CGSizeMake(100, 100) contentMode:1 options:requestOptions resultHandler:^(UIImage * _Nullable result, NSDictionary * _Nullable info) {
            
            
            [self.arrImage addObject:result];
            
            /**判断数组是否遍历*/
                if (i == array.count - 1) {
                    
            /**利用通知中心将遍历后的数组传到 Fifth 界面.  */
                [[NSNotificationCenter defaultCenter] postNotificationName:@"getAllPic" object:nil userInfo:@{@"data":self.arrImage,@"count":[NSNumber numberWithInteger:array.count],@"arrayForAsset":array}];
                
            }
            
            
        }];

    }
    
    [fethOptions release];
    
    
}

- (void)button2Action:(UIButton *)button {
    
   
    
    
}

- (void)button3Action:(UIButton *)button {
    
    CollectViewController *collect = [[CollectViewController alloc] init];
    
    id object = [self nextResponder];
    
    while (![object isKindOfClass:[Fifth class]] && object != nil) {
        
        object = [object nextResponder];
        
        
    }
    
    Fifth *fif = (Fifth *)object;
    
    [fif.navigationController pushViewController:collect animated:YES];
    
    
    [collect release];

    
    
}

- (void)button4Action:(UIButton *)button {
    
    
    
}





@end
